package com.example.zyfypt_611cgt.activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.ActionMenuItemView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import com.example.zyfypt_611cgt.R;
import com.example.zyfypt_611cgt.common.Common;
import com.example.zyfypt_611cgt.iface.CollectListener;
import com.example.zyfypt_611cgt.iface.FocusListener;
import com.example.zyfypt_611cgt.model.CollectModel;
import com.example.zyfypt_611cgt.model.FocusModel;

public class ViewArticleActivity extends AppCompatActivity {
    private WebView webView;
    private int resid, authorid;

    Context context;

    private Boolean flagcollect=false;//收藏标志
    private Boolean flagfocus=false;

    private CollectModel collectmodel;//收藏model
    private FocusModel focusmodel;

    private SharedPreferences sp;//简单存储
    private String sessionID="";  //sessionid
    private ActionMenuItemView item;
    private ActionMenuItemView item2;


    CollectListener listener=new CollectListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            if (item==null)
                item=findViewById(R.id.menusc);//注意菜单id

            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----收藏成功");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "1":System.out.println("----收藏失败");
                    break;
                case "4":System.out.println("----取消收藏成功");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                case "3":System.out.println("----取消收藏失败");
                    break;
                case "5":System.out.println("----已收藏");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "6":System.out.println("----未收藏");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    FocusListener listener2=new FocusListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            if (item2==null)
                item2=findViewById(R.id.menugz);//注意菜单id

            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----关注成功");
                    flagfocus=true;
                    item2.setTitle("取消关注");
                    break;
                case "1":System.out.println("----关注失败");
                    break;
                case "4":System.out.println("----取消关注成功");
                    flagfocus=false;
                    item2.setTitle("关注");
                    break;
                case "3":System.out.println("----取消关注失败");
                    break;
                case "5":System.out.println("----已关注");
                    flagfocus=true;
                    item2.setTitle("取消关注");
                    break;
                case "6":System.out.println("----未关注");
                    flagfocus=false;
                    item2.setTitle("关注");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_article);

        context = ViewArticleActivity.this;

        initViews();

        sp=context.getSharedPreferences("login",MODE_PRIVATE);//注意文件名
        readSP();//读取sessionid

    }

    private void readSP() {
        sessionID=sp.getString("session",null);//注意名值对的名
    }


    private void initViews() {
        webView = findViewById(R.id.webview);
        resid = getIntent().getIntExtra("resid", -1);
        authorid = getIntent().getIntExtra("authorid", -1);

        webView.loadUrl(Common.ARTICLEURL + resid);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { //菜单第一次创建初始化
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menucollect, menu);

        collectmodel=new CollectModel();//实例化对象
        collectmodel.exist("article",resid,sessionID,listener);//判断是否收藏

        focusmodel=new FocusModel();//实例化对象
        focusmodel.exist("userfocus",authorid,sessionID,listener2);//判断是否关注


        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {  //菜单被选择
        switch(item.getItemId()){
            case R.id.menusc:
                if(flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("article",resid,sessionID,listener);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("article",resid,sessionID,listener);
                }

                break;
            case R.id.menugz:
                if(flagfocus)//如果已关注，则调用取消收藏
                {
                    System.out.println("----准备取消关注");
                    focusmodel.unfocus("userfocus",authorid,sessionID,listener2);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备关注");
                    focusmodel.focus("userfocus",authorid,sessionID,listener2);
                }

                break;

        }

        return super.onOptionsItemSelected(item);
    }
}